package indi.h.mlx.analysises;

import java.lang.reflect.AnnotatedElement;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

import indi.h.mlx.analysises.inter.Analyser;
import indi.h.mlx.analysises.inter.Analysis;
import indi.h.mlx.core.bean.Column;
import indi.h.mlx.core.bean.Table;

public class ClassAnalyser implements Analyser<Class<?>, Table> {
	public Analysis<AnnotatedElement, Table> classAnalysis;
	public Analysis<Method, Column> methodAnalysis;
	public Analysis<Field, Column> fieldAnalysis;

	public ClassAnalyser(Analysis<AnnotatedElement, Table> classAnalysis, Analysis<Method, Column> methodAnalysis,
			Analysis<Field, Column> fieldAnalysis) {
		super();
		this.classAnalysis = classAnalysis;
		this.methodAnalysis = methodAnalysis;
		this.fieldAnalysis = fieldAnalysis;
	}

	@Override
	public Table work(Class<?> t) {
		// TODO Auto-generated method stub
		if (classAnalysis.isAnalysis(t)) {
			Table table = classAnalysis.analysis(t);
			Field[] declaredFields = t.getDeclaredFields();
			for (Field f : declaredFields){
				if (fieldAnalysis.isAnalysis(f)) {
					Column column = fieldAnalysis.analysis(f);
					column.getcColumn().columnType().classify(table, column);
				}
			}
			Method[] methods = t.getMethods();
			for (Method method : methods) {
				if (methodAnalysis.isAnalysis(method)) {
					Column column = methodAnalysis.analysis(method);
					column.getcColumn().columnType().classify(table, column);
				}
			}
			return table;
		}
		return null;
	}

}
